home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / M / MenuMaze.cpt / MenuMaze / MenuMaze.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-07  |  5.1 KB  |  260 lines  |  [TEXT/MSWD]

  1. /* MenuMaze.c by David Oster and Matthew Grayson
  2.  * June 6, 1987
  3.  * Synopsis: This program is an example of how to write hierarchical menus
  4.  * and why you should't. Hierarchical menus are a feature in the ROMs of the
  5.  * Macintosh SE and the Mac II. They are available to mere mortals who are
  6.  * using Apple's System 4.1 system software and who have the 128K ROMs:
  7.  * People who have either a MacPlus or a 512K Mac, Enhanced.
  8.  *
  9.  * This program compiles under LightSpeed C version 2.01.
  10.  * This program may be freely redistributed. It is hereby placed in the
  11.  * public domain. So There.
  12.  */
  13.  
  14. #include "ControlMgr.h"
  15. #include "DialogMgr.h"
  16. #include "EventMgr.h"
  17. #include "MacTypes.h"
  18. #include "MemoryMgr.h"
  19. #include "MenuMgr.h"
  20. #include "QuickDraw.h"
  21. #include "StdFilePkg.h"
  22. #include "TextEdit.h"
  23. #include "WindowMgr.h"
  24. #include "OSUtil.h"
  25.  
  26. #define NOT !
  27.  
  28. #define NIL 0L
  29.  
  30. #define Integer int
  31. typedef long int LongInt;
  32.  
  33. /* some 128k Rom mac defines */
  34. #define OldRomBit 0x8000
  35. extern int RomVersion : 0x28E;
  36. extern int MenuHeight : 0xBAA;
  37.  
  38. /* trap numbers of some new traps */
  39. #define UNIMPL 0x9F
  40. #define NEWTE 0x3D
  41.  
  42.  
  43.  
  44. enum {
  45.  APPLEMENU = 1,
  46.  FILEMENU,
  47.  EDITMENU
  48. };
  49.  
  50. typedef EventRecord *EventPtr;
  51. #define ABOUTI 1        /* About Grid Pic… */
  52.  
  53.  
  54. enum {
  55.  rABOUT = 131
  56. };
  57.  
  58. /* globals: contains screen in grid form */
  59. WindowPtr myWindow;
  60. Ptr buffer;
  61.  
  62. void GoMouseDown(), GoKey(), GoMenuBar(), GoCommand(), DoAppleMenu(),
  63.     DoDeskAcc(), DoFileMenu(), DoEditMenu(), DoExitMenu();
  64.  
  65.  
  66. void DoQuit(){
  67.     ExitToShell();
  68. }
  69.  
  70. void NoGo(alertId)Integer alertId;{
  71.     StopAlert(alertId, NIL);
  72.     DoQuit();
  73. }
  74.  
  75. main(){
  76.     void Init(), OneEvent(), GoIdle();
  77.  
  78.     Init();
  79.     while(TRUE){
  80.           OneEvent();
  81.         SystemTask();
  82.         GoIdle();
  83.     }
  84. }
  85.  
  86. /* OneEvent - dispatch on events, do idle processing
  87.  */
  88.  
  89. void OneEvent(){
  90.     void DrawBar(), HideBar();
  91.     EventRecord myEvent;
  92.   
  93.     if(GetNextEvent(everyEvent, &myEvent)){
  94.         switch(myEvent.what){
  95.             case updateEvt:    GoUpdateEvent( &myEvent);
  96.                 break;
  97.             case mouseDown:    GoMouseDown( &myEvent);
  98.                 break;
  99.         }
  100.     }
  101. }
  102.  
  103. /* GoUpdateEvent - update that window
  104.  */
  105. GoUpdateEvent(theEvent)EventPtr theEvent;{
  106.     Ptr bufP;    /* point into picture buffer */
  107.     GrafPtr savePtr;
  108.  
  109.     if(myWindow == (WindowPtr) theEvent->message){
  110.         GetPort(&savePtr);
  111.         SetPort( (WindowPtr) theEvent->message);
  112.         BeginUpdate(thePort);
  113.         EndUpdate(thePort);
  114.         SetPort(savePtr);
  115.     }
  116. }
  117.  
  118. /* GoMouseDown - handle mouse down from event manager
  119.  */
  120.  
  121. void GoMouseDown(theEvent)EventPtr theEvent;{
  122.     void GoContent();
  123.     WindowPtr whichWindow;
  124.  
  125.     switch(FindWindow(theEvent->where, &whichWindow)){
  126.         case inMenuBar : GoMenuBar(theEvent);
  127.                 break;
  128.         case inSysWindow :     SystemClick(theEvent, whichWindow);
  129.                 break;
  130.         case inContent : GoContent(theEvent);
  131.                 break;
  132.     }
  133. }
  134.  
  135. /* Init - Start things off
  136.  *
  137.  * We init managers we don't need, (Maybe a desk accessory will)
  138.  */
  139.  
  140. void Init(){
  141.     Integer i;
  142.  
  143.     FlushEvents(everyEvent, 0);
  144.     InitGraf(&thePort);
  145.     InitFonts();
  146.     InitWindows();
  147.     InitCursor();
  148.     InitDialogs(DoQuit);
  149.     InitMenus();
  150.     TEInit();
  151.     SetMenuBar(GetNewMBar(128));
  152.     AddResMenu(GetMHandle(APPLEMENU), 'DRVR');
  153.     if(RomVersion & OldRomBit)
  154.         NoGo(128);
  155.     if(NGetTrapAddress(UNIMPL, ToolTrap) == NGetTrapAddress(NEWTE, ToolTrap))
  156.         NoGo(129);
  157.     for(i = 19;GetResource('MENU', i) != NIL;i++){
  158.         InsertMenu(GetMenu(i), -1);
  159.     }
  160.     for(i = 31;GetResource('MENU', i) != NIL;i++){
  161.         InsertMenu(GetMenu(i), -1);
  162.     }
  163.     DrawMenuBar();
  164. }
  165.  
  166. void GoContent(theEvent)EventPtr theEvent;{
  167.     Point where;
  168.     where = theEvent->where;
  169.     GlobalToLocal(&where);
  170. /*    ClearMarquee(); */
  171. /*    SetMarquee(where); */
  172. }
  173. /* GoIdle - spin that marquee
  174.  */
  175.  
  176. void GoIdle(){
  177. /*    IdleMarquee(); */
  178. }
  179. /* GoMenuBar - handle mousedown in menu bar
  180.  */
  181. void GoMenuBar(theEvent)EventPtr theEvent;{
  182.     GoCommand(MenuSelect(theEvent->where));
  183. }
  184. /* GoCommand - dispatches on the menu.
  185.  * It guarantees that the item in the menubar is
  186.  * hilighted long enough to be able to see it.
  187.  */
  188. #define QUITI 2
  189. #define EXITMENU 22
  190.  
  191. void  GoCommand(theCom)LongInt theCom;{
  192.     LongInt endTime, newTime;
  193.     Integer theLow;
  194.     Boolean dontCare;
  195.     
  196.     theLow = LoWord(theCom);
  197.     endTime = 5 + TickCount();
  198.     switch(HiWord(theCom)){
  199.         case APPLEMENU :     DoAppleMenu(theLow);
  200.             break;
  201.         case EDITMENU :        dontCare = SystemEdit(theLow -1);
  202.             break;
  203.         case EXITMENU :     DoExitMenu(theLow);
  204.     }
  205.     newTime = TickCount();
  206.     if(newTime < endTime){            /* let's see that command */
  207.         Delay(endTime - newTime, &newTime);
  208.     }
  209.     HiliteMenu(0);
  210. }
  211.  
  212. /* DoAppleMenu - handle mouse down in apple menu
  213.  */
  214.  
  215. void DoAppleMenu(theItem)Integer theItem;{
  216.     Rect r;
  217.     switch(theItem){
  218.         case ABOUTI:
  219.             theItem = Alert(rABOUT, NIL);
  220.             break;
  221.         default :        DoDeskAcc(theItem);
  222.     }
  223. }
  224.  
  225.  
  226. /* DoDeskAcc - run a desk accessory
  227.  */
  228.  
  229. void DoDeskAcc(theItem)Integer theItem;{
  230.     Str255 accessory;
  231.  
  232.     GetItem(GetMHandle(APPLEMENU), theItem, accessory);
  233.     theItem = OpenDeskAcc(accessory);    /* don't care about return value */
  234. }
  235.  
  236. /* DoFileMenu -
  237.  */
  238.  
  239. void DoFileMenu(theItem)Integer theItem;{
  240.     void OpenGrid(), OpenPaint(), SavePaint(), SaveGrid(),
  241.         ToPaint(), ToGrid(), DoQuit();
  242.  
  243.     switch(theItem){
  244.         default:    DoQuit();
  245.     }
  246. }
  247.  
  248. /* DoEditMenu -
  249.  */
  250.  
  251. void DoExitMenu(theItem)Integer theItem;{
  252.     switch(theItem){
  253.         case QUITI:    
  254.             Alert(130, NIL);
  255.             DoQuit();
  256.     }
  257. }
  258.  
  259.  
  260.